package com.dashu.authority.servie.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.dashu.authority.common.Constant;
import com.dashu.authority.common.MessageCode;
import com.dashu.authority.entity.Menu;
import com.dashu.authority.exception.AuthorityException;
import com.dashu.authority.repository.MenuMapper;
import com.dashu.authority.servie.MenuService;
import com.dashu.authority.util.AuthorityServiceUtil;
import com.github.pagehelper.PageHelper;

@Service
public class MenuServiceImpl  extends BaseServiceImpl<Menu> implements MenuService {
	@Autowired
	private MenuMapper menuMapper;

	@Override
	public List<Menu> queryAllByExample(Menu entity, int pageNum, int pageSize) throws AuthorityException {
		PageHelper.startPage(pageNum, pageSize);
		Map<Object, Object> params = new HashMap<Object, Object>();
		if (entity!=null) {
			if (entity.getApplicationId()!=null) {
				params.put("applicationId", entity.getApplicationId());
			}
			if (entity.getMenuName()!=null && entity.getMenuName()!="") {
				params.put("menuName", entity.getMenuName());
			}
			
		}
		params.put("orderBy", "create_time");
		params.put("sort", "desc");
		List<Menu> list = menuMapper.queryAllByExample(params);
		if (list!=null && list.size()>0) {
			for (Menu menu : list) {
				if (menu.getParentId()!=null) {
					String parentName = menuMapper.findMenuNameByPerantId(menu.getParentId());
					menu.setParentName(parentName);
				}
			}
		}
		return list;
	}

	@Override
	public Map<String, Object> insertEntity(Menu entity) throws AuthorityException {
		if (entity.getParentId()==null) { //为父级
			entity.setParentId(0);
		}
		int ret = menuMapper.insertEntity(entity);
		if(ret>0){
			return AuthorityServiceUtil.OperaSuccessMap(Constant.OPERATION_SAVE);
		}else{
			return AuthorityServiceUtil.OperaFailMap(Constant.OPERATION_SAVE);
		}
	}

	@Override
	public Map<String, Object> updateEntity(Menu entity) throws AuthorityException {
		int ret = menuMapper.updateEntity(entity);
		if(ret>0){
			return AuthorityServiceUtil.OperaSuccessMap(Constant.OPERATION_SAVE);
		}else{
			return AuthorityServiceUtil.OperaFailMap(Constant.OPERATION_SAVE);
		}
	}

	@Override
	public Map<String, Object> deleteByIds(Integer[] ids) throws AuthorityException {
		if(ids==null || ids.length==0){
			throw new AuthorityException(MessageCode.ERROR_PARAMS_NULL.toString(),"信息id数组:");
		}
		Map<Object,Object> params=new HashMap<Object,Object>();
		params.put("ids", ids);
		int ret=menuMapper.deleteByIds(params);
		if(ret>0){
			return AuthorityServiceUtil.OperaSuccessMap(Constant.OPERATION_DELETE);
		}else {
			return AuthorityServiceUtil.OperaFailMap(Constant.OPERATION_DELETE);
		}
	}
	
	@Override
	public Map<String, Object> updateStatus(Integer[] ids,String status) throws AuthorityException {
		if(ids==null || ids.length==0){
			throw new AuthorityException(MessageCode.ERROR_PARAMS_NULL.toString(),"信息id数组:");
		}
		Map<Object,Object> params=new HashMap<Object,Object>();
		params.put("ids", ids);
		params.put("status", status);
		int ret=menuMapper.updateStatus(params);
		if(ret>0){
			return AuthorityServiceUtil.OperaSuccessMap(Constant.OPERATION_DELETE);
		}else {
			return AuthorityServiceUtil.OperaFailMap(Constant.OPERATION_DELETE);
		}
	}

	@Override
	public List<Menu> queryIdNotSamePId(Menu entity, int pageNum, int pageSize) throws AuthorityException {
		PageHelper.startPage(pageNum, pageSize);
		Map<Object, Object> params = new HashMap<Object, Object>();
		if (entity!=null) {		
		}
		params.put("orderBy", "create_time");
		params.put("sort", "desc");
		List<Menu> list = menuMapper.queryIdNotSamePId(params);
		if (list!=null && list.size()>0) {
			for (Menu menu : list) {
				if (menu.getParentId()!=null) {
					String parentName = menuMapper.findMenuNameByPerantId(menu.getParentId());
					menu.setParentName(parentName);
				}
			}
		}
		return list;
	}

	
	
	/**
     * 
     * @param dList
     * @return
     */
	public void getTreeNodeData(Menu dt){
        //List<Menu> userList=menuMapper.queryMenutChildrenById(dt.getId().toString());
      /*  List<Menu> t=dt.getMenuChildrenList();
        if(t!=null && t.size()>0){
            t.addAll(userList);
            for(Menu t1:t){
                getTreeNodeData(t1);
            }
        }else{
            dt.setMenuChildrenList(userList);
        }*/
    }

	@Override
	public List<Menu> queryMenuTreeList(Map<Object, Object> params) throws AuthorityException {
		return menuMapper.queryMenuTreeList(params);	
	}
	
}
